package cn.com.ccxcredit.wallet;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.security.SecureRandom;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ccxcredit.wallet.app.RApplication;
import cn.com.ccxcredit.wallet.ui.MakeWalletActivity;
import cn.com.ccxcredit.wallet.ui.TransferActivity;
import cn.com.ccxcredit.wallet.ui.TransferKSActivity;
import credit.ccx.com.walletcore.coin.CoinType;
import credit.ccx.com.walletcore.util.MyMnemonicUtils;
import credit.ccx.com.walletcore.util.SecureRandomUtils;
import huixue.cn.com.library.base.SimpleActivity;

public class IndexActivity extends SimpleActivity {

    @BindView(R.id.btn_ethm)
    Button btnEthm;
    @BindView(R.id.btn_btc)
    Button btnBtc;
    @BindView(R.id.btn_word)
    Button btnWord;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout mFlowlayout;
    @BindView(R.id.btn_trans)
    Button btnTrans;
    @BindView(R.id.btn_key_trans)
    Button btnKeyTrans;


    @Override
    public int getLayout() {
        return R.layout.activity_index;
    }

    @Override
    public void initView() {

    }

    @Override
    public void addEvent() {

    }

    @OnClick({R.id.btn_ethm, R.id.btn_btc, R.id.btn_word, R.id.btn_trans,R.id.btn_key_trans})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_ethm: {
                Intent intent = new Intent(IndexActivity.this, MakeWalletActivity.class);
                intent.putExtra("coin", CoinType.COIN_TYPE_ETH.getName());
                startActivity(intent);
            }
            break;

            case R.id.btn_btc: {
                Intent intent = new Intent(IndexActivity.this, MakeWalletActivity.class);
                intent.putExtra("coin", CoinType.COIN_TYPE_BTC.getName());
                startActivity(intent);
            }
            break;

            case R.id.btn_word: {
                makeMnemonic();
            }
            break;

            case R.id.btn_trans: {
                /*try{
                    Web3j web3j = Web3jFactory.build(new HttpService("http://10.0.5.43:8101/"));
                    Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
                    String clientVersion = web3ClientVersion.getWeb3ClientVersion();
                    Log.d("ChannelIndex","clientVersion=="+clientVersion);
                }catch ( Exception e){
                    e.printStackTrace();
                }*/
                Intent intent = new Intent(IndexActivity.this, TransferActivity.class);
                startActivity(intent);
            }

            case R.id.btn_key_trans:{
                Intent intent = new Intent(IndexActivity.this, TransferKSActivity.class);
                startActivity(intent);
            }
            break;
        }
    }

    private void makeMnemonic() {
        byte[] initialEntropy = new byte[16];
        SecureRandom secureRandom = SecureRandomUtils.secureRandom();
        secureRandom.nextBytes(initialEntropy);
        //读记助词
        String mnemonic = MyMnemonicUtils.generateMnemonic(initialEntropy);
        RApplication.mnemonic = mnemonic;
        String[] mData = mnemonic.split(" ");

        mFlowlayout.setAdapter(new TagAdapter<String>(mData) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv, mFlowlayout, false);
                tv.setText(s);
                return tv;
            }
        });
    }


}
